import MovingDirectioin from './MovingDirection.js';


export default class Pacman {
    // column*this.tileSize,row*this.tileSize,this.tileSize, velocity
    constructor(x, y, tileSize, velocity, tileMap) {
        this.x = x
        this.y = y
        this.tileSize = tileSize
        
        this.velocity = velocity
        this.tileMap = tileMap
        //移动方向
        this.currentMovingDirection = null;
        //请求移动方向
        this.requestedMovingDirection = null;


        //吃豆人张嘴动画循环周期
        this.pacmanAnimationTimerDefault = 10; 
        this.pacmanAnimationTimer = null;


        //监听键盘事件
        document.addEventListener('keydown', this.#keydown)

        this.#loadPacmanImages();
    }

    //
    Rotation = {
        right: 0,
        down: 1,
        left: 2,
        up:3
    }

    //吃豆人动作的总方法
    draw(ctx) {
        this.#move()
        this.#animate()
        ctx.drawImage(this.pacmanImages[this.pacmanImageIndex], this.x, this.y, this.tileSize, this.tileSize)
    }

    //封装吃豆人张嘴图片循环（闭嘴，小张嘴，大张嘴，小张嘴）的数据
    #loadPacmanImages() {
        const pacmanImage1 = new Image();
        pacmanImage1.src = "../images/pac0.png"
        const pacmanImage2 = new Image();
        pacmanImage2.src = "../images/pac1.png"
        const pacmanImage3 = new Image();
        pacmanImage3.src = "../images/pac2.png"
        const pacmanImage4 = new Image();
        pacmanImage4.src = "../images/pac1.png"

        this.pacmanImages = [pacmanImage1, pacmanImage2, pacmanImage3, pacmanImage4]

        this.pacmanImageIndex = 0
    }
    /**
     * 
     * @param {*} event 
     * 键盘操作吃豆人移动的方向
     */

    #keydown =(event) => {
        console.log(this.currentMovingDirection !== this.requestedMovingDirection)
        //up
        if (event.keyCode == 38) {
            console.log('UP',MovingDirectioin.up)
            if (this.currentMovingDirection == MovingDirectioin.down)                 
                this.currentMovingDirection = MovingDirectioin.up
            this.requestedMovingDirection = MovingDirectioin.up

            

        }
        //down
        if (event.keyCode == 40) {
            console.log(40)
            if (this.currentMovingDirection == MovingDirectioin.up) 
                this.currentMovingDirection = MovingDirectioin.down
            this.requestedMovingDirection = MovingDirectioin.down
            
        }
        //left
        if (event.keyCode == 37) {
            if (this.currentMovingDirection == MovingDirectioin.right) 
                this.currentMovingDirection = MovingDirectioin.left
            this.requestedMovingDirection = MovingDirectioin.left
            
        }
        //right
        if (event.keyCode == 39) {
            if (this.currentMovingDirection == MovingDirectioin.left) 
                this.currentMovingDirection = MovingDirectioin.right
            this.requestedMovingDirection = MovingDirectioin.right
            
        }
    }
/**
 * 
 * @returns 
 * 吃豆人移动碰撞检测，移动
 */
    #move() {
     
        //当前运动方向和请求运动方向不同时，当前运动方向改为请求运动方向
        if (this.currentMovingDirection !== this.requestedMovingDirection) {

            if (Number.isInteger(this.x / this.tileSize) && Number.isInteger(this.y / this.tileSize)) {
                
                if(!this.tileMap.didCollideWithEnvironment(this.x,this.y,this.requestedMovingDirection))

                this.currentMovingDirection = this.requestedMovingDirection;
            }
           
        }
        
        //运动方向撞墙时
        if (this.tileMap.didCollideWithEnvironment(this.x, this.y, this.currentMovingDirection)) {
            //停止张嘴动画
            this.pacmanAnimationTimer = null;
            this.pacmanImageIndex = 1;

            //跳出方法，停止移动
            return;

        } else if (this.currentMovingDirection != null && this.pacmanAnimationTimer == null) {

            //否则 设置开始张嘴动画时间
            this.pacmanAnimationTimer = this.pacmanAnimationTimerDefault;
        }

        //运动过程，x,y坐标变化
        switch (this.currentMovingDirection) {
            case MovingDirectioin.up:
                this.y -= this.velocity;
                break;
            case MovingDirectioin.down:
                this.y += this.velocity;
                break;
            case MovingDirectioin.left:
                this.x -= this.velocity;
                break;
            case MovingDirectioin.right:
                this.x += this.velocity;
                break;
        }
    }

    //每隔（pacmanAnimationTimerDefault）10毫秒换一帧，吃豆人张嘴动画
    #animate() {
        if (this.pacmanAnimationTimer == null) {
            return;
        }

        //计时
        this.pacmanAnimationTimer--;
    
        if (this.pacmanAnimationTimer == 0) {
            this.pacmanAnimationTimer = this.pacmanAnimationTimerDefault

            //换一帧图片
            this.pacmanImageIndex++;
            if (this.pacmanImageIndex == this.pacmanImages.length) {

                //重置图片到第一帧
                this.pacmanImageIndex = 0;
            }
        }
    }
}

